{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 灰色关联分析法\n",
    "\n",
    "[参考链接](https://www.cnblogs.com/aabbcc/p/9747715.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "灰色关联系数矩阵:\n",
      "[[0.33333333 0.47058824 1.         0.53333333 0.4        0.66666667]\n",
      " [0.40458015 0.76811594 0.33333333 1.         0.53535354 0.62352941]\n",
      " [0.56       0.33333333 0.48275862 0.60869565 0.4516129  1.        ]\n",
      " [0.33333333 0.43396226 0.46938776 0.51111111 1.         0.58974359]\n",
      " [0.48       0.6        0.36363636 1.         0.48       0.33333333]\n",
      " [0.52380952 1.         0.47826087 0.33333333 0.52380952 0.64705882]\n",
      " [0.6        0.38461538 1.         0.42857143 0.33333333 0.375     ]\n",
      " [0.51724138 0.33333333 0.71428571 1.         0.38461538 0.65217391]\n",
      " [0.41463415 0.68       1.         0.45945946 0.33333333 0.62962963]]\n",
      "灰色加权关联度:  [0.46299243 0.55599428 0.64907363 0.6527227  0.493562   0.61301504]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "data = np.loadtxt('14.D 供应商选择决策.txt')\n",
    "# 效益型指标标准化\n",
    "for index in [0]+[i for i in range(4, 9)]:\n",
    "    data[index] = (data[index] - np.min(data[index])) / (np.max(data[index]) - np.min(data[index]))\n",
    "# 成本型指标标准化\n",
    "for index in [i for i in range(1, 4)]:\n",
    "    data[index] = (np.max(data[index]) - data[index]) / (np.max(data[index]) - np.min(data[index]))\n",
    "\n",
    "best = np.array([np.max(data[index]) for index in range(data.shape[0])])\n",
    "# 由于做了scale上句和下句等效\n",
    "# best = np.ones(data.shape[0])\n",
    "\n",
    "mmax = np.max(data.max(axis=1))\n",
    "mmin = np.min(data.min(axis=1))\n",
    "rho = 0.5\n",
    "GRC = np.divide((mmin + rho * mmax), ((np.array([best]).T - data) + rho * mmax))\n",
    "print('灰色关联系数矩阵:', GRC, sep='\\n')\n",
    "\n",
    "# 计算灰色加权关联度, 取等权重\n",
    "w = np.ones(GRC.shape[0])/GRC.shape[0]\n",
    "R = np.matmul(w, GRC)\n",
    "print('灰色加权关联度: ', R)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
